package org.jeecg.modules.bim.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.bim.entity.BimDevs;
import org.jeecg.modules.bim.mapper.IBimDevsMapper;
import org.jeecg.modules.bim.service.IBimDevsService;
import org.jeecg.modules.core.constant.YesOrNotEnum;
import org.jeecg.modules.project.entity.ProjectInfo;
import org.jeecg.modules.project.service.IProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class BimDevsServiceImpl extends ServiceImpl<IBimDevsMapper, BimDevs> implements IBimDevsService {

    @Autowired
    private IProjectInfoService projectInfoService;

    @Override
    @Transactional
    public BimDevs addBimDevs(BimDevs bimDevs) {
        ProjectInfo projectInfo = projectInfoService.getById(bimDevs.getProjectInfoId());
        if (Objects.isNull(projectInfo)) {
            log.error("添加bim设备数据,未查询到对应系统项目信息,projectInfoId=>{}", bimDevs.getProjectInfoId());
            throw new JeecgBootException("对应工程不存在");
        }
        bimDevs.setProjectId(bimDevs.getProjectId());
        this.save(bimDevs);
        return bimDevs;
    }

    @Override
    public BimDevs devsByProjectInfoId(Integer projectInfoId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(BimDevs::getProjectInfoId, projectInfoId)
                .one();
    }

    @Override
    public List<BimDevs> queryDevs(Integer projectInfoId, Long projectId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(projectInfoId != null, BimDevs::getProjectInfoId, projectInfoId)
                .eq(projectId != null, BimDevs::getProjectId, projectId)
                .list();
    }

    @Override
    public List<BimDevs> queryDevsList(Integer projectInfoId, Long projectId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(projectInfoId != null, BimDevs::getProjectInfoId, projectInfoId)
                .eq(projectId != null, BimDevs::getProjectId, projectId)
                .list();
    }

}
